<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="父主键" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节点类型" prop="ctype">
        <el-select v-model="queryParams.ctype" placeholder="请选择节点类型" clearable>
          <el-option
            v-for="dict in dict.type.accompany_hospital_dict_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="祖级列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖级列表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在省市">
        <el-cascader
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.cascadeValue"
          placeholder="选择所在城市"
          :options="cityDictOptions"
          filterable></el-cascader>
      </el-form-item>
      <el-form-item label="等级" prop="grade">
        <el-select v-model="queryParams.grade" placeholder="请选择等级" clearable>
          <el-option
            v-for="dict in dict.type.accompany_hospital_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.accompany_hospital_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="about">
        <el-input
          v-model="queryParams.about"
          placeholder="请输入简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重点科室" prop="keyDepartments">
        <el-input
          v-model="queryParams.keyDepartments"
          placeholder="请输入重点科室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['accompany:hospital:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <u-table
      ref="plTreeTable"
      fixed-columns-roll
      beautify-table
      header-drag-style
      :height="height"
      :treeConfig="{
   children: 'children',
   iconClose: 'el-icon-arrow-right',
   iconOpen: 'el-icon-arrow-down',
   expandAll: false,
              lazy: true,
              load: load,
              hasChildren: 'hasChildren'}"
      use-virtual
      row-id="id"
      border>
      <u-table-column :tree-node="true" width="200" align="center" label="名称" prop="name"
                      show-overflow-tooltip/>
<!--      <u-table-column label="名称" align="center" prop="name"/>-->
      <u-table-column label="节点类型" align="center" prop="ctype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_hospital_dict_type" :value="scope.row.ctype"/>
        </template>
      </u-table-column>
      <u-table-column label="id" align="center" prop="id"></u-table-column>
      <u-table-column label="父id" align="center" prop="parentId"></u-table-column>
      <u-table-column label="省" align="center" prop="provinceName"></u-table-column>
      <u-table-column label="城市" align="center" prop="cityName"></u-table-column>
      <u-table-column label="地址" align="center" prop="address"></u-table-column>
      <u-table-column label="等级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_hospital_grade" :value="scope.row.grade"/>
        </template>
      </u-table-column>
      <u-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_hospital_type" :value="scope.row.type"/>
        </template>
      </u-table-column>
      <u-table-column label="简介" align="center" prop="about"/>
      <u-table-column label="重点科室" align="center" prop="keyDepartments"/>
      <u-table-column label="简介" align="center" prop="about"/>
      <u-table-column label="简介" align="center" prop="about"/>
      <u-table-column label="封面图片" align="center" prop="coverPicture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverPicture" :width="50" :height="50"/>
        </template>
      </u-table-column>
      <u-table-column label="备注" align="center" prop="remark"/>
      <u-table-column label="操作" align="center" class-name="small-padding" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:bizselfdomcustom:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['business:bizselfdomcustom:add']"
            v-if="scope.row.ctype == 0 || scope.row.ctype == 1"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:bizselfdomcustom:remove']"
          >删除
          </el-button>
        </template>
      </u-table-column>
    </u-table>

    <!--<el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="hospitalList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="节点类型" align="center" prop="ctype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_hospital_dict_type" :value="scope.row.ctype"/>
        </template>
      </el-table-column>
      <el-table-column label="祖级列表" align="center" prop="ancestors" />
      <el-table-column label="所在省" align="center" prop="provinceName" />
      <el-table-column label="所在城市" align="center" prop="cityName" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="等级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_hospital_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_hospital_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="about" />
      <el-table-column label="重点科室" align="center" prop="keyDepartments" />
      <el-table-column label="封面图片" align="center" prop="coverPicture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverPicture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['accompany:hospital:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['accompany:hospital:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accompany:hospital:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>-->

    <!-- 添加或修改医院信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父主键" prop="parentId">
          <treeselect v-model="form.parentId" :options="hospitalOptions" :normalizer="normalizer" placeholder="请选择父主键" />
        </el-form-item>
        <el-form-item label="节点类型" prop="ctype">
          <el-select v-model="form.ctype" placeholder="请选择节点类型">
            <el-option
              v-for="dict in dict.type.accompany_hospital_dict_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
       <!-- <el-form-item label="所在省" prop="provinceLive">
          <el-input v-model="form.provinceLive" placeholder="请输入所在省" />
        </el-form-item>-->
        <el-form-item label="所在城市" prop="cityLive">
         <!-- <el-input v-model="form.cityLive" placeholder="请输入所在城市" />-->
          <el-cascader
            v-model="form.cascadeValue"
            placeholder="选择所在城市"
            :options="cityDictOptions"
            filterable></el-cascader>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="等级" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择等级">
            <el-option
              v-for="dict in dict.type.accompany_hospital_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.accompany_hospital_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="about">
          <el-input v-model="form.about" type="textarea" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="重点科室" prop="keyDepartments">
          <el-input v-model="form.keyDepartments" placeholder="请输入重点科室" />
        </el-form-item>
        <el-form-item label="封面图片" prop="coverPicture">
          <image-upload v-model="form.coverPicture"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHospital, getHospital, delHospital, addHospital, updateHospital } from "@/api/accompany/hospital";
import {getCityTree} from "@/api/accompany/accompany_common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Hospital",
  dicts: ['accompany_hospital_type', 'accompany_hospital_grade', 'accompany_hospital_dict_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      height: 700,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 医院信息表格数据
      hospitalList: [],
      // 城市信息树选项
      cityDictOptions: [],
      // 医院信息树选项
      hospitalOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        ctype: null,
        ancestors: null,
        name: null,
        provinceLive: null,
        cityLive: null,
        address: null,
        grade: null,
        type: null,
        about: null,
        keyDepartments: null,
        cascadeValue: [],
        coverPicture: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        ctype: [
          { required: true, message: "节点类型不能为空", trigger: "blur" }
        ],
       /* address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "等级不能为空", trigger: "blur" }
        ],*/
      }
    };
  },
  created() {
    this.getCityDictOptions();
    this.getList("0");
  },
  methods: {
    load(row, resolve){
      let query = {
        parentId: row.id
      }
      listHospital(query).then(response => {
        response.data.forEach(info => {
          if(info.ctype == 0 || info.ctype == 1){
            info.hasChildren = true;
          }
        })
        resolve(response.data);
      });
    },
    /** 查询城市信息树*/
    getCityDictOptions(){
      getCityTree().then(resp=>{
        console.log("城市信息树",resp.data)
        this.cityDictOptions = resp.data
      })
    },
    /** 查询医院信息列表 */
    getList(parentId) {
      this.loading = true;
      // 判断选择了省市
      if(this.queryParams.cascadeValue && this.queryParams.cascadeValue.length >0){
        this.queryParams.cityLive = this.queryParams.cascadeValue[1]
      }
      console.log("parentId",parentId)
      /*if(parentId){
        console.log("111111111111111")
        this.queryParams.parentId = parentId;
      }*/
      this.queryParams.parentId = parentId;
      listHospital(this.queryParams).then(response => {
        this.hospitalList = response.data;
        /*this.hospitalList = this.handleTree(response.data, "id", "parentId");
        console.log("数据列表",this.hospitalList);
        this.loading = false;*/
        this.hospitalList.forEach(info => {
          if(info.ctype == 0 || info.ctype == 1){
            info.hasChildren = true;
          }
        })
        // 设置表格数据
        this.$refs.plTreeTable.reloadData(this.hospitalList)
        this.loading = false;
      });
    },
    /** 转换医院信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
	/** 查询医院信息下拉树结构 */
    getTreeselect() {
      listHospital().then(response => {
        this.hospitalOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.hospitalOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        ctype: null,
        ancestors: null,
        name: null,
        provinceLive: null,
        cityLive: null,
        address: null,
        grade: null,
        type: null,
        about: null,
        keyDepartments: null,
        coverPicture: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList(null);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getList("0");
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.getCityDictOptions();
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加医院信息";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getCityDictOptions();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getHospital(row.id).then(response => {
        this.form = response.data;
        this.form.cascadeValue = []
        this.form.cascadeValue[0] = response.data.provinceLive+""
        this.form.cascadeValue[1] = response.data.cityLive+""

        this.open = true;
        this.title = "修改医院信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.cascadeValue && this.form.cascadeValue.length>0){
            this.form.provinceLive = this.form.cascadeValue[0];
            this.form.cityLive = this.form.cascadeValue[1];
          }else if(this.form.ctype == '0'){ // 节点是医院 需要填写城市
            return this.$modal.msgError("请选择所在城市");
          }
          console.log("数据是",this.form);
          if (this.form.id != null) {
            updateHospital(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList("0");
            });
          } else {
            addHospital(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList("0");
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除医院信息编号为"' + row.id + '"的数据项？').then(function() {
        return delHospital(row.id);
      }).then(() => {
        this.getList("0");
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
